=begin

This file is part of the RSIF project, http://rsif.googlecode.com

Copyright (c) 2009-2009 Roberto Dario Seminara

rsif is free software: you can redistribute it and/or modify
it under the terms of the gnu general public license as published by
the free software foundation, either version 3 of the license, or
(at your option) any later version.

rsif is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.  see the
gnu general public license for more details.

you should have received a copy of the gnu general public license
along with rsif.  if not, see <http://www.gnu.org/licenses/>.


=end
require "base/main/main.rb"

class BasicHttpConfiguration < Main

	condition do |context|

		args = context.symbol( :args )

		url = args.find{|x| x =~ /http\:\/\//}

		if url
			# verificar que la url tiene al menos el parametro
			arguments = BasicHttpConfiguration.read_arguments(url)
			arguments ? 1 : nil
		else
			nil
		end
	end

	def self.read_arguments( url )
		spl = url.split("?")
		if spl.length < 2
			nil
		else
			parameters = spl[1]

			spl2 = parameters.split("&")
			if spl2.map{|param| param =~ /=/ }.inject{|x,y| x and y}

				param_hash = Hash.new

				lastparameter = nil
				lastvalue = nil

				spl2.each do |x|
					key, value = x.split("=")
					param_hash[key] = value

					lastparameter, lastvalue = key, value
				end

				param_hash = param_hash.select {|key,value| key != lastparameter }

				{ 
				:base_url => spl[0], 
				:parameters => param_hash, 
				:strparameters => parameters,
				:input_parameter => lastparameter,
				:input_true => lastvalue
				}
			else
				nil
			end
		end
	end

	def run( context )
		args = context.symbol( :args )

		url = args.find{|x| x =~ /http\:\/\//}

		if url
			# verificar que la url tiene al menos el parametro
			arguments = BasicHttpConfiguration.read_arguments(url)

			# setear los parametros de conexion http
			context.define_symbol( :http_base_url , arguments[:base_url] )
			context.define_symbol( :http_str_get_parameters , arguments[:strparameters] )
			context.define_symbol( :http_get_parameters , arguments[:parameters] )
			context.define_symbol( :http_input_get_parameter , arguments[:input_parameter] )

			# setear los parametros de funcionamiento de blind
			context.define_symbol( :blind_input_true , arguments[:input_true] )
		end
	end
end

